fix(dashboard): price session, project, and branch costs per turn model - #165
Open
blineadam wants to merge 2 commits into
Open
fix(dashboard): price session, project, and branch costs per turn model#165blineadam wants to merge 2 commits into
blineadam wants to merge 2 commits into
Conversation
…uryn#160) /api/data now ships each session a per-model token breakdown (by_model, grouped from turns), and a sessionCost() helper sums calcCost over it. Recent Sessions, Cost by Project, and Cost by Project & Branch previously billed a session's total tokens at its single primary model, so sub-agent turns on cheaper models were charged at the main model's rate. Falls back to primary-model pricing when the breakdown is empty (old DBs).
This was referenced Jul 27, 2026
sessionCost() prices each model in a session's by_model breakdown separately, so a session can be billable even when its primary model isn't (e.g. a non-billable main model that dispatched a billable sub-agent). The Recent Sessions table still gated its n/a display on isBillable(s.model) alone, so that real cost was computed but hidden. sessionIsBillable() checks the whole breakdown instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #160.
The Recent Sessions, Cost by Project, and Cost by Project & Branch tables were pricing all of a session’s tokens using its primary model. That meant sub-agent turns on cheaper models were still being charged at the main model’s rate.
The top Est. Cost card already prices each turn using its actual model, which is why the totals did not match. In one real Fable session with 27 Fable turns and 46 Sonnet turns, the session table showed $8.72 instead of the correct $6.01. Most of that difference came from Sonnet cache reads being priced at Fable’s higher rate.
Implementation
/api/datanow adds aby_modeltoken breakdown to each session using the model already stored for every turn in theturnstable.A new
sessionCost()helper in the dashboard JavaScript calculates the session total by pricing each model’s share separately. It replaces the existing primary-model calculation in:If a session does not have a model breakdown, it falls back to the existing primary-model calculation. This keeps databases without turn-level records working as before.
Nothing in the database needs to change. Existing history will be repriced automatically as soon as the updated code runs, with no rescan required.
Verification
Added three regression tests in
tests/test_dashboard.pycovering multi-model sessions:by_modelincludes each model used by the sessionThe full test suite passes with 150 tests.
I also ran the extracted dashboard JavaScript in Node and confirmed that a mixed Fable and Sonnet session is priced as the sum of each model’s usage. The empty-breakdown fallback also produces the same value as the previous calculation.
Finally, I verified the fix against a real database. The session, project, and project-and-branch tables now match the top Est. Cost card.